package com.jeesite.modules.jydq.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.jydq.entity.JydqCategory;
import com.jeesite.modules.jydq.entity.JydqPlatform;
import com.jeesite.modules.jydq.entity.JydqProductPrice;
import com.jeesite.modules.jydq.service.JydqCategoryService;
import com.jeesite.modules.jydq.service.JydqProductPriceService;
import com.jeesite.modules.sys.utils.RoleUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.jydq.entity.JydqProduct;
import com.jeesite.modules.jydq.service.JydqProductService;

/**
 * 商品信息表Controller
 * @author dong
 * @version 2025-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/jydq/product")
public class JydqProductController extends BaseController {

	@Autowired
	private JydqProductService jydqProductService;
	@Autowired
	private JydqCategoryService jydqCategoryService;
	@Autowired
	private JydqProductPriceService jydqProductPriceService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public JydqProduct get(String id, boolean isNewRecord) {
		return jydqProductService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
//	@RequiresPermissions("jydq:product:view")
	@RequestMapping(value = {"list", ""})
	public String list(JydqProduct jydqProduct, Model model) {
		model.addAttribute("jydqProduct", jydqProduct);
		return "modules/jydq/jydqProductList";
	}
	
	/**
	 * 查询列表数据
	 */
//	@RequiresPermissions("jydq:product:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<JydqProduct> listData(JydqProduct jydqProduct, HttpServletRequest request, HttpServletResponse response) {
		jydqProduct.setPage(new Page<>(request, response));
		Page<JydqProduct> page = jydqProductService.findPage(jydqProduct);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
//	@RequiresPermissions("jydq:product:view")
	@RequestMapping(value = "form")
	public String form(JydqProduct jydqProduct, Model model) {
		if (!jydqProduct.getIsNewRecord()) {
			if (!RoleUtils.hasCurrentUserRole("corpAdmin") && !RoleUtils.hasCurrentUserRole("manage")) {
				jydqCategoryService.addViewCount(jydqProduct.getCategoryId());
				jydqCategoryService.addComparisonsCount(jydqProduct.getCategoryId());
				Map<String, List<JydqProductPrice>> prices = jydqProductPriceService.findStatistics(jydqProduct.getId());
				jydqProduct.setPrices(prices);
			}
			if (StringUtils.isNotEmpty(jydqProduct.getSpecifications())){
				List<Map<String, String>> list = new ArrayList<>();
				for (String s : jydqProduct.getSpecifications().split(";")) {
					String[] split = s.split(":");
					if (split.length > 1) {
						Map<String, String> map = new HashMap<>();
						map.put("name", split[0]);
						map.put("value", split[1]);
						list.add(map);
					}
				}
				jydqProduct.setSpecificationList(list);
			}
			if (StringUtils.isNotEmpty(jydqProduct.getRemarks())){
				List<Map<String, String>> list = new ArrayList<>();
				for (String s : jydqProduct.getRemarks().split(";")) {
					String[] split = s.split(":");
					if (split.length > 1) {
						Map<String, String> map = new HashMap<>();
						map.put("name", split[0]);
						map.put("value", split[1]);
						list.add(map);
					}
				}
				jydqProduct.setCharacteristic(list);
			}
		}
		model.addAttribute("jydqProduct", jydqProduct);
		return "modules/jydq/jydqProductForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("jydq:product:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated JydqProduct jydqProduct) {
		jydqProductService.save(jydqProduct);
		return renderResult(Global.TRUE, text("保存商品信息表成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("jydq:product:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(JydqProduct jydqProduct) {
		jydqProductService.delete(jydqProduct);
		return renderResult(Global.TRUE, text("删除商品信息表成功！"));
	}

	/**
	 * 查询下拉框选项
	 */
	@RequestMapping({"treeData"})
	@ResponseBody
	public List<Map<String, Object>> treeData(JydqProduct jydqProduct) {
		List<Map<String, Object>> list = new ArrayList<>();
		List<JydqProduct> list1 = jydqProductService.findList(jydqProduct);
		for (JydqProduct data : list1) {
			HashMap<String, Object> map = new HashMap<>();
			map.put("id",data.getId());
			map.put("name",data.getName());
			list.add(map);
		}
		return list;
	}

	/**
	 * 我收藏的商品
	 */
	@RequestMapping(value = "myFavorite")
	@ResponseBody
	public Page<JydqProduct> myFavorite(JydqProduct jydqProduct, HttpServletRequest request, HttpServletResponse response) {
		jydqProduct.setPage(new Page<>(request, response));
		Page<JydqProduct> page = jydqProductService.myFavorite(jydqProduct);
		return page;
	}
	
}